package shinesend.com.cn.surfaceviewdemo;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.IOException;

/**
 * surfaceView 内部维持一个双缓冲机制
 * 要显示的内容准备-->显示到界面--->再次准备要显示的内容--->再次显示到界面 : 依次类推
 * 过程:
 * A线程准备数据---->A线程获得数据来到主线程,显示到界面上来
 *                     B线程准备数据
 *                                  -->B线程获得的数据来到主线程,显示到界面上来
 *                                      C准备数据
 *                                          .....
 *
 */

public class MainActivity extends AppCompatActivity {

    private EditText et_path;
    //播放界面surfaceView
    private SurfaceView sv;
    //http://blog.sina.com.cn/s/blog_79dfe1270101canv.html
    private SurfaceHolder holder;//接口;监听surface的
    //音频视频的播放
    private MediaPlayer mediaPlayer;
    //暂停
    private Button bt_pause;
    //记录播放位置
    private int position ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView(){
        bt_pause = (Button) findViewById(R.id.bt_pause);
        et_path = (EditText) findViewById(R.id.et_path);
        sv = (SurfaceView) findViewById(R.id.sv);
        //获得界面显示的容器;
        holder = sv.getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {
            //当界面可见从新开始;
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                if (position>0){
                    try {
                        mediaPlayer = new MediaPlayer();
                        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                        mediaPlayer.setDisplay(holder);
                        mediaPlayer.setDataSource(path);
                        mediaPlayer.prepare(); // might take long! (for buffering, etc)
                        mediaPlayer.start();
                        mediaPlayer.seekTo(position);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                //屏幕分辨率 横屏竖屏被切换了,播放器大小变化了

            }

            @Override//当界面不可见 就会被销毁掉
            public void surfaceDestroyed(SurfaceHolder holder) {
                if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
                    position = mediaPlayer.getCurrentPosition();
                    mediaPlayer.stop();
                    mediaPlayer.release();
                }
            }
        });
    }
    public void pause(View view) {
        if ("继续".equals(bt_pause.getText().toString())){
            //暂停状态
            mediaPlayer.start();
            return;
        }
        if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
            mediaPlayer.pause();
            bt_pause.setText("继续");
        }
    }
    String path;
    public void play(View view) {
        path = et_path.getText().toString();
        if (TextUtils.isEmpty(path)){
            Toast.makeText(getApplicationContext(), "路径不能为空", Toast.LENGTH_LONG).show();
            return;
        }
        mediaPlayer = new MediaPlayer();
        //设置Io流的类型;
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        //绑定接口
        mediaPlayer.setDisplay(holder);
        //设置路径
        try {
            mediaPlayer.setDataSource(path);
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void stop(View view) {
        if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
            mediaPlayer.stop();
        }
    }
    public void replay(View view) {
        if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
            mediaPlayer.seekTo(0);
        }
    }
}
